package vn.intelsoft.vlogvn.adapter;

import java.util.ArrayList;
import java.util.List;



import vn.intelsoft.vlogvn.R;
import vn.intelsoft.vlogvn.VlogActivity;
import vn.intelsoft.vlogvn.imageloader.ImageLoader;
import vn.intelsoft.vlogvn.instance.Instance;
import vn.intelsoft.vlogvn.instance.Vlog;
import vn.intelsoft.vlogvn.instance.Vlogger;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter implements OnClickListener {

	private List<Vlogger> vloggers;
	private Context mContext;
	private ImageLoader imageLoader;
	public ListAdapter(Context context, List<Vlogger> vloggers) {
		this.mContext = context;
		this.vloggers = vloggers;
		imageLoader = new ImageLoader(mContext);
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (this.vloggers.size() % 2 == 0)
			return this.vloggers.size() / 2;
		return 1 + this.vloggers.size() / 2;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, null);
			viewHolder = new ViewHolder();
			viewHolder.layout1 = (RelativeLayout) convertView.findViewById(R.id.layout1);
			viewHolder.layout2 = (RelativeLayout) convertView.findViewById(R.id.layout2);
			
			viewHolder.imageview1 = (ImageView) convertView.findViewById(R.id.imageView1);
			viewHolder.imageview2 = (ImageView) convertView.findViewById(R.id.imageView2);
			viewHolder.textview1 = (TextView) convertView.findViewById(R.id.textView1);
			viewHolder.textview2 = (TextView) convertView.findViewById(R.id.textView2);
			
			LayoutParams layoutParams = (LayoutParams) viewHolder.imageview1.getLayoutParams();
			layoutParams.height = Instance.width(mContext) / 2;
			viewHolder.imageview1.setLayoutParams(layoutParams);
			viewHolder.imageview2.setLayoutParams(layoutParams);
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		List<Vlogger> vlogger2 = get2Vlogger(this.vloggers, position);
		viewHolder.layout1.setOnClickListener(this);
		viewHolder.layout1.setTag(vlogger2.get(0));
		viewHolder.textview1.setText(vlogger2.get(0).name);
		imageLoader.DisplayImage(vlogger2.get(0).thumb, viewHolder.imageview1, 350, 350);
		if(vlogger2.size() == 2){
			viewHolder.textview2.setText(vlogger2.get(1).name);
			viewHolder.layout2.setOnClickListener(this);
			viewHolder.layout2.setTag(vlogger2.get(1));
			imageLoader.DisplayImage(vlogger2.get(1).thumb, viewHolder.imageview2, 350, 350);
		}
		
		return convertView;
	}
	
	private class ViewHolder{
		ImageView imageview1, imageview2;
		TextView textview1, textview2;
		RelativeLayout layout1, layout2;
	}
	
	private List<Vlogger> get2Vlogger(List<Vlogger> vloggers, int position){
		List<Vlogger> vloggers2 = new ArrayList<Vlogger>();
		if(position == 0){
			vloggers2.add(vloggers.get(0));
			vloggers2.add(vloggers.get(1));
		}else{
			vloggers2.add(vloggers.get(position * 2));
			if(position * 2 < vloggers.size() - 1){
				vloggers2.add(vloggers.get(position * 2 + 1 ));
			}
		}
		
		return vloggers2;
	}

	@Override
	public void onClick(View v) {
		Vlogger vlogger = (Vlogger) v.getTag();
		
		Intent it = new Intent(mContext, VlogActivity.class);
		it.putExtra("name", vlogger.name);
		it.putExtra("username", vlogger.username);
		mContext.startActivity(it);
	}

}
